<template>
  <div class="consult-detail-page" v-if="consultOrder.id">
    <nav-bar title="问诊详情" />
    <!-- 头部部分 -->
    <div class="detail-head">
      <div class="text">
        <h3>图文问诊 {{ consultOrder.payment }} 元</h3>
        <span class="sta">{{ consultOrder.statusValue }}</span>
        <p class="tip">服务医生信息</p>
      </div>
      <div class="card">
        <img class="avatar" src="@/assets/images/avatar-doctor.svg" alt="" />
        <p class="doc">
          <span>极速问诊</span>
          <span>{{ consultOrder.docInfo?.name || '暂未分配医生' }}</span>
        </p>
        <van-icon name="arrow" />
      </div>
    </div>
    <div class="detail-patient" v-if="consultOrder.patientInfo">
      <van-cell-group>
        <van-cell
          title="患者信息"
          :value="
            `${consultOrder.patientInfo.name} | ${consultOrder.patientInfo.genderValue} | ${consultOrder.patientInfo.age}岁`
          "
        />
        <van-cell
          title="患病时长"
          :value="getIllnessTimeText(consultOrder.illnessTime)"
        />
        <van-cell
          title="就诊情况"
          :value="getConsultFlagText(consultOrder.consultFlag)"
        />
        <van-cell title="病情描述" :label="consultOrder.illnessDesc"></van-cell>
      </van-cell-group>
    </div>
    <div class="detail-order">
      <h3>订单信息</h3>
      <van-cell-group>
        <van-cell title="订单编号">
          <template #value>
            <span class="copy" @click="copy">复制</span>
            <span style="margin-left:10px;" ref="orderNoRef">{{
              consultOrder.orderNo
            }}</span>
          </template></van-cell
        >
        <van-cell title="创建时间" :value="consultOrder.createTime" />
        <van-cell title="应付款" :value="`￥${consultOrder.payment}`" />
        <van-cell
          title="优惠券"
          :value="`-￥${consultOrder.couponDeduction}`"
        ></van-cell>
        <van-cell
          title="积分抵扣"
          :value="`-￥${consultOrder.pointDeduction}`"
        ></van-cell>
        <van-cell
          title="实付款"
          :value="`￥${consultOrder.actualPayment}`"
          class="price"
        ></van-cell>
      </van-cell-group>
    </div>
    <div
      class="detail-time"
      v-if="consultOrder.status === OrderType.ConsultPay"
    >
      请在
      <van-count-down :time="consultOrder.countdown * 1000" />
      内完成支付，超时订单将取消
    </div>
    <div
      class="detail-action van-hairline--top"
      v-if="consultOrder.status === OrderType.ConsultPay"
    >
      <div class="price">
        <span>需付款</span>
        <span>￥{{ consultOrder.actualPayment }}</span>
      </div>
      <van-button
        type="default"
        round
        :loading="loading"
        @click="cancelOrder(consultOrder)"
        >取消问诊</van-button
      >
      <van-button type="primary" round @click="show = true"
        >继续支付</van-button
      >
    </div>
    <div
      class="detail-action van-hairline--top"
      v-if="consultOrder.status === OrderType.ConsultWait"
    >
      <van-button
        type="default"
        round
        :loading="loading"
        @click="cancelOrder(consultOrder)"
        >取消问诊</van-button
      >
      <van-button type="primary" round :to="`/room?orderId=${consultOrder.id}`"
        >继续沟通</van-button
      >
    </div>
    <div
      class="detail-action van-hairline--top"
      v-if="consultOrder.status === OrderType.ConsultChat"
    >
      <van-button type="default" v-if="consultOrder.prescriptionId" round
        >查看处方</van-button
      >
      <van-button type="primary" round :to="`/room?orderId=${consultOrder.id}`"
        >继续沟通</van-button
      >
    </div>
    <div
      class="detail-action van-hairline--top"
      v-if="consultOrder.status === OrderType.ConsultComplete"
    >
      <div class="more">
        <van-popover
          placement="top-start"
          v-model:show="showPopover"
          :actions="actions"
          @select="onSelect"
        >
          <template #reference> 更多 </template>
        </van-popover>
      </div>
      <van-button type="default" round :to="`/room?orderId=${consultOrder.id}`"
        >问诊记录</van-button
      >
      <van-button type="primary" round v-if="consultOrder.evaluateId"
        >写评价</van-button
      >
      <van-button type="default" round v-else>查看评价</van-button>
    </div>
    <div
      class="detail-action van-hairline--top"
      v-if="consultOrder.status === OrderType.ConsultCancel"
    >
      <van-button
        round
        :loading="deleteLoading"
        @click="deleteOrder(consultOrder.id)"
        >删除订单</van-button
      ><van-button type="primary" round @click="$router.push('/')"
        >咨询其它医生</van-button
      >
    </div>
    <!-- 支付面板 -->
    <pay-sheet
      v-model:show="show"
      :order-id="consultOrder.id"
      :actualPayment="consultOrder.actualPayment"
      :payCallback="payCallback"
      :onClose="onClose"
    />
  </div>
  <div class="consult-detail-page" v-else>
    <nav-bar title="问诊详情" />
    <van-skeleton title :row="4" style="margin-top: 30px" />
    <van-skeleton title :row="4" style="margin-top: 30px" />
  </div>
</template>

<script lang="ts" setup>
// 导入子组件
import { onMounted, ref } from 'vue'
import { ApiRes } from '@/types/data'
import { ConsultOrderItem } from '@/types/consult'
import request from '@/utils/request'
import { useRoute, useRouter } from 'vue-router'
import { getIllnessTimeText, getConsultFlagText } from '@/utils/filter'
import { Toast } from 'vant'
import { useDeleteOrder, useCancelOrder, useShowPrescription } from '@/hook'
import { OrderType } from '@/enums'
const route = useRoute()
const router = useRouter()
const { loading, cancelOrder } = useCancelOrder()
const { loading: deleteLoading, deleteOrder } = useDeleteOrder(() => {
  router.back()
})
const consultOrder = ref({} as ConsultOrderItem)
const getConsultOrder = async () => {
  const res = await request.get<ApiRes<ConsultOrderItem>>(
    'patient/consult/order/detail',
    {
      params: {
        orderId: route.params.id
      }
    }
  )

  if (res.data.code === 10000) {
    consultOrder.value = res.data.data
  }
}
onMounted(() => {
  getConsultOrder()
})

const showPopover = ref(false)
const { showPrescription } = useShowPrescription()
const actions = [{ text: '查看处方' }, { text: '删除订单' }]
const onSelect = (action: { text: string }, i: number) => {
  if (i === 0) {
    showPrescription(consultOrder.value.prescriptionId)
  }
  if (i === 1) {
    // 删除
    deleteOrder(consultOrder.value.id)
  }
}

// 复制
const orderNoRef = ref()
const copy = () => {
  var text = orderNoRef.value.innerText

  var input = document.createElement('input')
  input.setAttribute('id', 'copyInput')

  input.setAttribute('value', text)

  document.getElementsByTagName('body')[0].appendChild(input)

  input.select()

  document.execCommand('copy')

  input.remove()

  Toast({ type: 'text', message: '复制成功~', duration: 1000 })
}
// 支付相关
const show = ref(false)
const payCallback = '/room'
const onClose = () => {
  show.value = false
  getConsultOrder()
}
</script>

<style lang="scss" scoped>
.consult-detail-page {
  padding: 46px 0 110px 0;
}
.detail-head {
  height: 140px;
  position: relative;
  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 135px;
    background: linear-gradient(
      180deg,
      rgba(44, 181, 165, 0),
      rgba(44, 181, 165, 0.2)
    );
    border-bottom-left-radius: 150px 20px;
    border-bottom-right-radius: 150px 20px;
  }
  padding: 15px;
  .text {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 10px 3px;
    .sta {
      color: var(--cp-tag);
      font-weight: 500;
      font-size: 16px;
      &.green {
        color: var(--cp-primary);
      }
      &.orange {
        color: #f2994a;
      }
    }
    .tip {
      width: 100%;
      color: var(--cp-text3);
      margin-top: 5px;
    }
  }
  .card {
    height: 74px;
    background-color: #fff;
    border-radius: 8px;
    position: relative;
    display: flex;
    align-items: center;
    padding: 0 15px;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    .avatar {
      width: 38px;
      height: 38px;
    }
    .doc {
      flex: 1;
      padding-left: 15px;
      > span {
        display: block;
        font-size: 16px;
        &:last-child {
          font-size: 13px;
          color: var(--cp-text3);
        }
      }
    }
    .van-icon {
      color: var(--cp-tip);
    }
  }
}
.detail-patient {
  &::after {
    content: '';
    display: block;
    height: 12px;
    background-color: var(--cp-bg);
  }
}
.detail-order {
  > h3 {
    padding: 10px 18px;
    font-weight: normal;
  }
  .copy {
    padding: 2px 10px;
    border: 1px solid var(--cp-primary);
    background-color: var(--cp-plain);
    color: var(--cp-primary);
    font-size: 12px;
    border-radius: 12px;
    margin-right: 10px;
  }
  :deep(.van-cell__title) {
    width: 70px;
    flex: none;
  }
  .price :deep(.van-cell__value) {
    font-size: 16px;
    color: var(--cp-price);
  }
}
.detail-action {
  height: 65px;
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  background-color: #fff;
  justify-content: flex-end;
  padding: 0 15px;
  box-sizing: border-box;
  .price {
    flex: 1;
    > span:last-child {
      font-size: 18px;
      color: var(--cp-price);
      padding-left: 5px;
    }
  }
  .van-button {
    margin-left: 10px;
    padding-left: 17px;
    padding-right: 17px;
  }
  :deep(.van-button--default) {
    background-color: var(--cp-bg);
    color: var(--cp-text3);
  }
  .more {
    flex: 1;
    color: var(--cp-tag);
  }
}
.van-cell {
  padding-left: 18px;
  padding-right: 18px;
}
.detail-time {
  position: fixed;
  left: 0;
  bottom: 65px;
  width: 100%;
  height: 44px;
  background-color: #fff7eb;
  text-align: center;
  line-height: 44px;
  font-size: 13px;
  color: #f2994a;
  .van-count-down {
    display: inline;
    color: #f2994a;
  }
}
</style>
